
import { _decorator, Component, Node } from 'cc';
const { ccclass, property } = _decorator;
import * as cc from "cc";
import { loaderMgr } from '../framework/loader/LoaderMgr';
import { loader } from '../framework/loader/AssetLoader';
import { resourcesMgr } from '../framework/loader/ResourcesMgr';

/**
 * Predefined variables
 * Name = LoaderTest
 * DateTime = Wed Mar 16 2022 17:34:56 GMT+0800 (中国标准时间)
 * Author = 272493431
 * FileBasename = LoaderTest.ts
 * FileBasenameNoExtension = LoaderTest
 * URL = db://assets/scripts/scene/LoaderTest.ts
 * ManualUrl = https://docs.cocos.com/creator/3.4/manual/zh/
 *
 */
 
@ccclass('LoaderTest')
export class LoaderTest extends cc.Component {
    


    


    start () {
    
        let funcs = [
            this.preload1,
            this.preload2,
            this.preloadDir1,
            this.load1,
            this.load2,
            this.loadArray2,
            this.loadDir1
        ];
        for (let i = 0; i < 7; i++) {
            let button = cc.find(`Canvas/Panel/Button${i+1}`);
            button.on(cc.Node.EventType.TOUCH_END,funcs[i].bind(this));
        }
        
    }
    

    // resources 普通资源预加载测试
    async preload1(){
        let res = await resourcesMgr.preload(LoaderTest,'goblins-pma',cc.SpriteFrame);

        let res2 = await resourcesMgr.preload(LoaderTest,'face_1',cc.SpriteFrame,(err,data)=>{
            console.log(`LoaderTest-> `);
        });

        let res3 = await resourcesMgr.preload(LoaderTest,'goblins',cc.sp.SkeletonData,(index,total,item)=>{
            console.log(`LoaderTest-> `);
        },(err,data)=>{
            console.log(`LoaderTest-> `);
        });


    }
    
    // bundle 普通资源预加载测试
    async preload2(){
        let res = await resourcesMgr.preload(LoaderTest,'images/skins/icon_cloth_1',cc.SpriteFrame,'test_asset');

        
        let res2 = await resourcesMgr.preload(LoaderTest,'images/skins/icon_cloth_4',cc.SpriteFrame,(err,data)=>{
            console.log(`LoaderTest-> `);
        },'test_asset');


        let res3 = await resourcesMgr.preload(LoaderTest,'images/common/btnHome',cc.SpriteFrame,(index,total,item)=>{
            console.log(`LoaderTest-> `);
        },(err,data)=>{
            console.log(`LoaderTest-> `);
        },'test_asset');
    }
    
    
    // bundle 文件夹资源加载测试
    preloadDir1(){

        resourcesMgr.preloadDir(LoaderTest,'images/common',cc.SpriteFrame,'test_asset');

        resourcesMgr.preloadDir(LoaderTest,'images/decor',cc.SpriteFrame,(err,data)=>{
            console.log(`LoaderTest-> `);
        },'test_asset');

        resourcesMgr.preloadDir(LoaderTest,'images/skins',cc.SpriteFrame,(index,total,item)=>{

            console.log(`LoaderTest-> `,index,"  ",total,"  ",item);
        },(err,data)=>{
            console.log(`LoaderTest-> `);
        },'test_asset');
    }
    
    preloadDir2(){

    }
    
    // 普通加载测试
    async load1(){
     
        let res = resourcesMgr.load(LoaderTest,'goblins-pma',cc.SpriteFrame);

        let res2 = resourcesMgr.load(LoaderTest,'face_1',cc.SpriteFrame,(err,data)=>{
            console.log(`LoaderTest-> `);
        });

        let res3 = resourcesMgr.load(LoaderTest,'goblins',cc.sp.SkeletonData,(index,total,item)=>{
            console.log(`LoaderTest-> `);
        },(err,data)=>{
            console.log(`LoaderTest-> `);
        });

        await this.sleep(5000);
        
        resourcesMgr.releaseAll(LoaderTest);

        console.log(`LoaderTest-> `);
    }
    // bundle 加载测试
    async load2(){
        let res = resourcesMgr.load(LoaderTest,'images/skins/icon_cloth_1',cc.SpriteFrame,'test_asset');

        let res2 = resourcesMgr.load(LoaderTest,'images/skins/icon_cloth_4',cc.SpriteFrame,(err,data)=>{
            console.log(`LoaderTest-> `);
        },'test_asset');


        let res3 = resourcesMgr.load(LoaderTest,'images/common/btnHome',cc.SpriteFrame,(index,total,item)=>{
            console.log(`LoaderTest-> `);
        },(err,data)=>{
            console.log(`LoaderTest-> `);
        },'test_asset');

        
        await this.sleep(5000);
        
        resourcesMgr.releaseAll(LoaderTest);

        console.log(`LoaderTest-> `);
    }
    // 普通数组测试
    async loadArray1(){
    }
    
    // bundle 数组测试
    async loadArray2(){
        
        let res = resourcesMgr.loadArray(LoaderTest,[
            'images/skins/icon_cloth_1',
            'images/skins/icon_cloth_2',
            'images/skins/icon_cloth_3',
            'images/skins/icon_cloth_4',
            'images/skins/icon_cloth_5',
        ],cc.SpriteFrame,'test_asset');

        let res2 = resourcesMgr.loadArray(LoaderTest,[
            'images/skins/icon_hair_0',
            'images/skins/icon_hair_1',
            'images/skins/icon_hair_2',
            'images/skins/icon_hair_3',
        ],cc.SpriteFrame,(err,data)=>{
            console.log(`LoaderTest-> `);
        },'test_asset');


        let res3 = resourcesMgr.loadArray(LoaderTest,[
            'images/decor/icon_carpet1',
            'images/decor/icon_carpet2',
            'images/decor/icon_carpet3',
            'images/decor/icon_carpet4',
        ],cc.SpriteFrame,(index,total,item)=>{
            console.log(`LoaderTest-> `);
        },(err,data)=>{
            console.log(`LoaderTest-> `);
        },'test_asset');

        
        await this.sleep(5000);
        
        resourcesMgr.releaseAll(LoaderTest);

        console.log(`LoaderTest-> `);
    }

    // bundle 文件夹测试
    async loadDir1(){
        
        resourcesMgr.loadDir(LoaderTest,'images/common',cc.SpriteFrame,'test_asset');

        resourcesMgr.loadDir(LoaderTest,'images/decor',cc.SpriteFrame,(err,data)=>{
            console.log(`LoaderTest-> `);
        },'test_asset');

        resourcesMgr.loadDir(LoaderTest,'images/skins',cc.SpriteFrame,(index,total,item)=>{

            console.log(`LoaderTest-> `,index,"  ",total,"  ",item);
        },(err,data)=>{
            console.log(`LoaderTest-> `);
        },'test_asset');
        await this.sleep(5000);
        
        resourcesMgr.releaseAll(LoaderTest);

        console.log(`LoaderTest-> `);
    }
    
    
    // 普通文件夹测试
    async loadDir2(){
        
    }
    

    sleep(timeout){
        return new Promise<void>((resolve, reject) => {
            setTimeout(() => {
                resolve();
            }, timeout);
        })
    }
}
